package com.leador.broadcase;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.util.Log;

import com.leador.core.Trace;
import com.leador.service.LBSTraceService;
import com.leador.utils.TraceSeleGenre;

/**
 * Created by liuha on 2016/9/13.
 */
public class NetBroadCastReceiver  extends BroadcastReceiver {

    private LBSTraceService lbsTraceService = null;
    private Handler handler = null;

    public NetBroadCastReceiver(LBSTraceService lbsTraceService) {
        this.lbsTraceService = lbsTraceService;
        this.handler = lbsTraceService.getLbsServiceHandler();
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if ((context == null) || (intent == null)) {
            return;
        }
        String action = intent.getAction();
        if ("android.net.conn.CONNECTIVITY_CHANGE".equals(action)) {
            ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            if ((networkInfo != null) && (networkInfo.isConnected()) && (networkInfo.isAvailable())) {
                if((TraceSeleGenre.DATA_HHTP.getValue() == Trace.traceType)){
                    new ReStartHttpThread(this.handler).start();
                };
                if(TraceSeleGenre.DATA_SOCKET.getValue() == Trace.traceType || TraceSeleGenre.NOT_DATA_SOCKET_CONNECT.getValue() == Trace.traceType){
                    new ReStartSocketThread(this.handler).start();
                }
                return;
            }else{
//                if((TraceSeleGenre.DATA_HHTP.getValue() == Trace.traceType)){
//                    this.handler.obtainMessage(142).sendToTarget();
//                };
            }
        }
    }
}
